package cn.zyq.demo.service.pay.impl;

import cn.hutool.core.math.MathUtil;
import cn.zyq.demo.beans.pay.PayOrder;
import cn.zyq.demo.beans.sys.SysAdmin;
import cn.zyq.demo.beans.user.User;
import cn.zyq.demo.mapper.pay.PayOrderMapper;
import cn.zyq.demo.service.pay.PayOrderService;
//import cn.zyq.demo.service.task.TaskService;
import cn.zyq.demo.system.common.BaseService;
import cn.zyq.demo.system.common.PageParam;
import cn.zyq.demo.system.enums.core.OrderTypeEnum;
import cn.zyq.demo.system.enums.core.ResponseCodeEnum;
import cn.zyq.demo.system.exception.BusinessException;
import cn.zyq.demo.system.pay.config.PayAliProperties;
import cn.zyq.demo.system.pay.config.PayWxProperties;
import cn.zyq.demo.system.pay.core.client.PayClient;
import cn.zyq.demo.system.pay.core.client.PayCommonResult;
import cn.zyq.demo.system.pay.core.client.dto.*;
import cn.zyq.demo.system.pay.core.client.impl.alipay.AlipayPayClientConfig;
import cn.zyq.demo.system.pay.core.client.impl.wx.WXPayClientConfig;
import cn.zyq.demo.system.pay.core.enums.PayChannelEnum;
import cn.zyq.demo.system.pay.core.enums.PayOrderStatusEnum;
import cn.zyq.demo.system.pay.core.enums.PayTradeStatusEnum;
import cn.zyq.demo.system.pay.core.utils.CommonResult;
import cn.zyq.demo.system.ry.util.SecurityUtils;
import cn.zyq.demo.system.utils.DateUtils;
import com.alibaba.fastjson.JSON;
import com.aliyun.oss.ServiceException;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付订单Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-06-01
 */
@Service
@Slf4j
public class PayOrderServiceImpl extends BaseService implements PayOrderService {

    @Autowired
    private PayOrderMapper payOrderMapper;


    /**
     * 查询支付订单
     *
     * @param payOrderID 支付订单主键
     * @return 支付订单
     */
    @Override
    public PayOrder selectPayOrderByPayOrderID(Integer payOrderID) {
        PayOrder payOrder = payOrderMapper.selectByPrimaryKey(payOrderID);
        if (payOrder.getDeleted() == 1) {
            return null;
        }
        return payOrder;
    }

    /**
     * 查询支付订单
     *
     * @param serviceID 业务ID
     * @return 支付订单
     */
    @Override
    public PayOrder detail(String serviceID) {
        PayOrder param = new PayOrder();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.payOrderMapper.selectOne(param);
    }

    /**
     * 查询支付订单列表
     *
     * @param pageParam
     * @return 支付订单
     */
    @Override
    public PageInfo<PayOrder> selectPayOrderList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<PayOrder> list = this.payOrderMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询支付订单列表
     *
     * @param param
     * @return 支付订单
     */
    @Override
    public List<PayOrder> selectPayOrderList(PayOrder param) {
        param.setDeleted(0);
        return this.payOrderMapper.select(param);
    }

    /**
     * 新增支付订单
     *
     * @param param 支付订单
     * @return 结果
     */
    @Override
    public int insertPayOrder(PayOrder param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.payOrderMapper.insertSelective(param);
    }

    /**
     * 修改支付订单
     *
     * @param param 支付订单
     * @return 结果
     */
    @Override
    public int updatePayOrder(PayOrder param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.payOrderMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除支付订单
     *
     * @param payOrderIDs 需要删除的支付订单主键
     * @return 结果
     */
    @Override
    public int deletePayOrderByPayOrderIDs(Integer[] payOrderIDs) {
        int i = 0;
        for (Integer payOrderID : payOrderIDs) {
            i += this.deletePayOrderByPayOrderID(payOrderID);
        }
        return i;
    }

    /**
     * 删除支付订单信息
     *
     * @param payOrderID 支付订单主键
     * @return 结果
     */
    @Override
    public int deletePayOrderByPayOrderID(Integer payOrderID) {
        PayOrder payOrder = this.selectPayOrderByPayOrderID(payOrderID);
        if (payOrder == null) {
            return 0;
        } else {
            PayOrder storeInfo = new PayOrder();
            storeInfo.setPayOrderID(payOrderID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.payOrderMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }


    @Override
    public PayOrder pay(PayOrder param, User user) {
        if (param.getOrderAmount() == null) {
            throw new BusinessException(ResponseCodeEnum.ERROR, "请填写完整参数");
        }
        //组装参数
        PayOrderUnifiedReqDTO reqDTO = new PayOrderUnifiedReqDTO();
        reqDTO.setAmount(MathUtil.yuanToCent(param.getOrderAmount().doubleValue()));
        reqDTO.setSubject(param.getSubject());
        reqDTO.setBody(param.getBodyInfo());
        reqDTO.setOutTradeNo(param.getOrderCode());
        reqDTO.setUserIp(param.getUsersIp());
        //设置过期时间
        if (param.getStatus() != null) {
            reqDTO.setExpireTime(DateUtils.localDateTime2Date(param.getExpireTime()));
        } else {
            reqDTO.setExpireTime(DateUtils.addMinutes(new Date(), 30));
        }
        PayClient client = null;
        //根据不同的支付途径进行不同渠道选择
        if (PayChannelEnum.getChannelIdByType(param.getChannelId()).equals("WX")) {
            //微信支付
            client = this.wxPay(reqDTO, param, user);

        } else if (PayChannelEnum.getChannelIdByType(param.getChannelId()).equals("ALI")) {
            //支付宝APP支付
            client = this.aliAppPay(reqDTO, param);
        } else if (PayChannelEnum.getChannelIdByType(param.getChannelId()).equals("LOCAL")) {
            //平台钱包支付
            // return this.localPay(param, user);
        } else {
            throw new BusinessException(ResponseCodeEnum.ERROR, "暂不支持该支付方式");
        }
        //发起下单请求
        CommonResult<?> result = client.unifiedOrder(reqDTO);
        param.setPayReqRespInfo(result.toString());
        PayOrderUnifiedRespDto data = (PayOrderUnifiedRespDto) result.getData();
        if (data == null || StringUtils.isBlank(data.getPayReqResp())) {
            param.setStatus(PayOrderStatusEnum.FAIL.getValue());
            this.setCreateBaseData(param, user);
            throw new BusinessException(ResponseCodeEnum.ERROR, "没有获取到微信预支付请求响应信息");
        }
        param.setStatus(PayOrderStatusEnum.PAYING.getValue());
        param.setExpireTime(DateUtils.date2LocalDateTime(reqDTO.getExpireTime()));
        param.setPayReqResp(data.getPayReqResp());
        this.setCreateBaseData(param, user);
        this.payOrderMapper.insertSelective(param);
        param = this.detail(param.getServiceID());
        param.setPayReqResp(data.getPayReqResp());
        return param;
    }

    /**
     * 微信app支付
     *
     * @param reqDTO
     * @param param
     * @return
     */
    private PayClient wxPay(PayOrderUnifiedReqDTO reqDTO, PayOrder param, User user) {
        reqDTO.setNotifyUrl(PayWxProperties.payNotifyUrl);
        WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
        amount.setTotal(param.getOrderAmount().intValue());
        amount.setCurrency("CNY");
        Map map = new HashMap();
        if (PayChannelEnum.WX_H5.getChannelId().equals(param.getChannelId())) {
            map.put("h5Info", JSON.toJSONString(amount));
        } else if (PayChannelEnum.WX_LITE.getChannelId().equals(param.getChannelId())) {
            if (StringUtils.isBlank(user.getWxMaOpenid())) {
                throw new BusinessException("请先绑定微信小程序");
            }
            map.put("openid", user.getWxMaOpenid());
        }

        reqDTO.setChannelExtras(map);
        // 获得支付渠道
        PayClient client = WXPayClientConfig.getPayClientByChannelId(param.getChannelId());
        if (client == null) {
            throw new BusinessException(ResponseCodeEnum.ERROR, "微信支付失败，没有获取到渠道信息");
        }
        reqDTO.setChannelId(client.getId().toString());
        // param.setAppid(client.getId());
        param.setMchid(PayWxProperties.mchId);
        param.setNotifyUrl(PayWxProperties.payNotifyUrl);
        return client;
    }

    /**
     * 阿里app支付
     *
     * @param reqDTO
     * @param param
     * @return
     */
    private PayClient aliAppPay(PayOrderUnifiedReqDTO reqDTO, PayOrder param) {
        PayClient client = AlipayPayClientConfig.getPayClientByChannelId(param.getChannelId());
        if (client == null) {
            throw new BusinessException(ResponseCodeEnum.ERROR, "支付宝支付失败，没有获取到渠道信息");
        }
        reqDTO.setNotifyUrl(PayAliProperties.payNotifyUrl);
        param.setAppid(PayAliProperties.androidAppid);
        param.setNotifyUrl(PayAliProperties.payNotifyUrl);
        return client;
    }

    @Override
    public Object wxPayNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("【请求开始】微信付款回调...");
        String body = null;
        try {
            body = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
        } catch (IOException e) {
            log.error("微信付款成功或失败回调失败：{}", "获取回调消息内容错误!");
            log.error(e.getMessage(), e);
            return WxPayNotifyResponse.fail(e.getMessage());
        }
        PayNotifyDataDTO payNotifyDataDTO = PayNotifyDataDTO.builder().body(body).build();
        PayClient client = WXPayClientConfig.getH5PayClient();
        //解析参数
        PayOrderNotifyRespDTO payOrderNotifyRespDTO = null;
        try {
            payOrderNotifyRespDTO = client.parseOrderNotify(payNotifyDataDTO);
            log.info("微信付款回调：{}", JSON.toJSONString(payNotifyDataDTO));
        } catch (Exception e) {
            log.error("微信付款回调失败，解析回调参数异常，参数是：{}", JSON.toJSONString(payNotifyDataDTO));
            log.error(e.getMessage(), e);
            return WxPayNotifyResponse.fail("付款失败");
        }
        if (payOrderNotifyRespDTO == null) {
            log.error("微信付款回调失败。解析回调参数为空");
            throw new ServiceException("回调失败。解析参数为空");
        }
        PayOrder updateInfo = new PayOrder();
        updateInfo.setNotifyInfo(payOrderNotifyRespDTO.getData());
        updateInfo.setChannelOrderNo(payOrderNotifyRespDTO.getChannelOrderNo());
        updateInfo.setNotifyStatus(payOrderNotifyRespDTO.getTradeStatus());
        updateInfo.setChannelUserId(payOrderNotifyRespDTO.getChannelUserId());
        updateInfo.setNotifyAmount(new BigDecimal(payOrderNotifyRespDTO.getTotalAmount()));
        updateInfo.setNotifyPayerAmount(new BigDecimal(payOrderNotifyRespDTO.getPayerTotalAmount()));
        updateInfo.setSuccessTime(DateUtils.date2LocalDateTime(payOrderNotifyRespDTO.getSuccessTime()));
        if (PayTradeStatusEnum.SUCCESS.getKey().equals(payOrderNotifyRespDTO.getTradeStatus())
                || PayTradeStatusEnum.TRADE_SUCCESS.getKey().equals(payOrderNotifyRespDTO.getTradeStatus())) {
            updateInfo.setStatus(PayOrderStatusEnum.NOTIFY_SUCCESS.getValue());
        } else {
            updateInfo.setStatus(PayOrderStatusEnum.NOTIFY_FAIL.getValue());
        }
        //获取原请求支付订单
        PayOrder dbPayOrder = this.payOrderMapper.selectByOrderCode(payOrderNotifyRespDTO.getOutTradeNo());
        if (dbPayOrder != null) {
            updateInfo.setPayOrderID(dbPayOrder.getPayOrderID());
            this.payOrderMapper.updateByPrimaryKeySelective(updateInfo);
            payOrderNotifyRespDTO.setRelatedOrderID(dbPayOrder.getRelatedOrderID());
        } else {
            log.info("没有找到原始订单");
            updateInfo.setOrderCode(payOrderNotifyRespDTO.getOutTradeNo());
            this.add(updateInfo, new SysAdmin());
        }
        synchronized (payOrderNotifyRespDTO.getOutTradeNo().intern()) {
            this.payNotifyOrder(payOrderNotifyRespDTO.getOutTradeNo());
        }
        return WxPayNotifyResponse.success("付款成功");
    }

    @Override
    public Object aliPayNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("【请求开始】支付宝付款回调...");
        String body = null;
        try {
            body = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
        } catch (IOException e) {
            log.error("支付宝付款成功或失败回调失败：{}", "获取回调消息内容错误!");
            log.error(e.getMessage(), e);
            return "fail";
        }
        PayNotifyDataDTO payNotifyDataDTO = PayNotifyDataDTO.builder().body(body).build();
        PayClient client = AlipayPayClientConfig.getH5PayClient();
        //解析参数
        PayOrderNotifyRespDTO payOrderNotifyRespDTO = null;
        try {
            payOrderNotifyRespDTO = client.parseOrderNotify(payNotifyDataDTO);
        } catch (Exception e) {
            log.error("支付宝付款回调失败，解析回调参数异常，参数是：{}", JSON.toJSONString(payNotifyDataDTO));
            log.error(e.getMessage(), e);
            return "fail";
        }
        if (payOrderNotifyRespDTO == null) {
            log.error("支付宝付款回调失败，解析回调参数为空");
            return "fail";
        }
        // log.info("支付宝付款写订单开始，参数：{}", JSON.toJSONString(payOrderNotifyRespDTO));
        PayOrder updateInfo = new PayOrder();
        updateInfo.setNotifyInfo(payOrderNotifyRespDTO.getData());
        updateInfo.setChannelOrderNo(payOrderNotifyRespDTO.getChannelOrderNo());
        updateInfo.setNotifyStatus(payOrderNotifyRespDTO.getTradeStatus());
        updateInfo.setChannelUserId(payOrderNotifyRespDTO.getChannelUserId());
        updateInfo.setNotifyAmount(new BigDecimal(payOrderNotifyRespDTO.getTotalAmount()));
        updateInfo.setNotifyPayerAmount(new BigDecimal(payOrderNotifyRespDTO.getPayerTotalAmount()));
        updateInfo.setSuccessTime(DateUtils.date2LocalDateTime(payOrderNotifyRespDTO.getSuccessTime()));
        if (PayTradeStatusEnum.SUCCESS.getKey().equals(payOrderNotifyRespDTO.getTradeStatus())
                || PayTradeStatusEnum.TRADE_SUCCESS.getKey().equals(payOrderNotifyRespDTO.getTradeStatus())) {
            updateInfo.setStatus(PayOrderStatusEnum.NOTIFY_SUCCESS.getValue());
        } else {
            updateInfo.setStatus(PayOrderStatusEnum.NOTIFY_FAIL.getValue());
        }
        //获取原请求支付订单
        PayOrder dbPayOrder = this.payOrderMapper.selectByOrderCode(payOrderNotifyRespDTO.getOutTradeNo());
        if (dbPayOrder != null) {
            updateInfo.setPayOrderID(dbPayOrder.getPayOrderID());
            this.payOrderMapper.updateByPrimaryKeySelective(updateInfo);
            payOrderNotifyRespDTO.setRelatedOrderID(dbPayOrder.getRelatedOrderID());
        } else {
            log.info("没有找到原始订单");
            updateInfo.setOrderCode(payOrderNotifyRespDTO.getOutTradeNo());
            this.add(updateInfo, new SysAdmin());
        }
        synchronized (payOrderNotifyRespDTO.getOutTradeNo().intern()) {
            this.payNotifyOrder(payOrderNotifyRespDTO.getOutTradeNo());
        }
        return "success";
    }


    private void add(PayOrder param, SysAdmin sysAdmin) {
        this.setUpdateBaseData(param, sysAdmin);
        this.payOrderMapper.updateByPrimaryKeySelective(param);
    }

    private void payNotifyOrder(String orderCode) {
        log.info("付款写订单开始，参数：{}", JSON.toJSONString(orderCode));
        PayOrder payOrder = this.payOrderMapper.selectByOrderCode(orderCode);
        if (payOrder == null) {
            //尝试使用ID
            log.error("回调失败，没有找到相关支付订单{}", orderCode);
            throw new ServiceException("回调失败，没有找到订单");
        }

         if (OrderTypeEnum.GM.getValue().equals(payOrder.getType())) {
             //购买道具
//             this.configPropService.payNotifyOrder(payOrder);
         } else if (OrderTypeEnum.FB.getValue().equals(payOrder.getType())) {
             //发布任务
//             this.taskService.payNotify(payOrder);
         } else {
             throw new ServiceException("没有找到对应的发放物品类型");
         }
    }

    /**
     * 退款
     *  @param orderCode    原始支付订单号
     * @param refundAmount 退款金额
     * @param refundReason 退款原因
     * @param user
     */
    @Override
    public PayOrder payRefund(String orderCode, BigDecimal refundAmount, String refundReason, String notifyUrl, User user) {
        //获取原始订单
        PayOrder dbPayOrder = this.payOrderMapper.selectByOrderCode(orderCode);
        this.checkLogicExist(dbPayOrder, "退款失败，没有获取到原支付订单");
        BigDecimal totalRefundAmount = refundAmount.add(dbPayOrder.getRefundAmount());
//        int totalRefundAmount = refundAmount.intValue() + dbPayOrder.getRefundAmount().intValue();
        if (totalRefundAmount.compareTo(dbPayOrder.getOrderAmount()) > 0){
            throw new BusinessException("退款金额超过订单支付金额");
        }
        // 获得支付渠道
        PayClient client = null;
        //根据不同的支付途径进行不同渠道选择
        if (PayChannelEnum.getChannelIdByType(dbPayOrder.getChannelId()).equals("WX")) {
            //微信支付
            client = WXPayClientConfig.getPayClientByChannelId(dbPayOrder.getChannelId());
            if (client == null) {
                throw new BusinessException(ResponseCodeEnum.ERROR, "微信退款失败，没有获取到渠道信息");
            }
        } else if (PayChannelEnum.getChannelIdByType(dbPayOrder.getChannelId()).equals("ALI")) {
            //支付宝APP支付
            client = AlipayPayClientConfig.getPayClientByChannelId(dbPayOrder.getChannelId());
            if (client == null) {
                throw new BusinessException(ResponseCodeEnum.ERROR, "支付宝退款失败，没有获取到渠道信息");
            }
        } else if (PayChannelEnum.getChannelIdByType(dbPayOrder.getChannelId()).equals("LOCAL")) {
            //平台钱包支付
            // return this.localPay(param, user);
            throw new BusinessException(ResponseCodeEnum.ERROR, "暂不支持该退款方式");
        } else {
            throw new BusinessException(ResponseCodeEnum.ERROR, "暂不支持该退款方式");
        }
        //创建订单号
        String refundId = "TK"+ user.getPhone() + "-" + System.currentTimeMillis();
        //组装退款参数
        PayRefundUnifiedReqDTO reqDTO = new PayRefundUnifiedReqDTO();
        reqDTO.setUserIp(dbPayOrder.getUsersIp());
        reqDTO.setChannelOrderNo(dbPayOrder.getChannelOrderNo());
        reqDTO.setMerchantRefundId(refundId);
        reqDTO.setPayTradeNo(orderCode);
        reqDTO.setAmount(refundAmount);
        reqDTO.setNotifyUrl(notifyUrl);
        PayCommonResult<PayRefundUnifiedRespDTO> result = client.unifiedRefund(reqDTO);
        PayOrder addOrder = this.getCreateInstance(PayOrder.class, user);
        addOrder.setPayReqRespInfo(result.toString());
        addOrder.setParentPayOrderID(dbPayOrder.getPayOrderID());
        addOrder.setRefundAmount(refundAmount);
        addOrder.setRefundReason(refundReason);
        addOrder.setNotifyUrl(notifyUrl);
        addOrder.setChannelId(dbPayOrder.getChannelId());
        PayRefundUnifiedRespDTO data =  result.getData();
        if (data == null || StringUtils.isBlank(data.getChannelRefundId())) {
            addOrder.setRefundStatus("FAIL");
            throw new BusinessException(ResponseCodeEnum.ERROR, "没有获取到微信预支付请求响应信息");
        }
        addOrder.setRefundStatus("SUCCESS");
        addOrder.setChannelRefundId(data.getChannelRefundId());
        this.payOrderMapper.insertSelective(addOrder);
        PayOrder updateOrder = this.getUpdateInstance(PayOrder.class, user);
        updateOrder.setPayOrderID(dbPayOrder.getPayOrderID());
        updateOrder.setRefundTimes(dbPayOrder.getRefundTimes() + 1);
        updateOrder.setRefundAmount(refundAmount.add( dbPayOrder.getRefundAmount()));
        this.payOrderMapper.updateByPrimaryKeySelective(updateOrder);
        return this.detail(dbPayOrder.getServiceID());
    }

    @Override
    public Object returnNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("【请求开始】退款回调...");
        String body = null;
        try {
            body = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
        } catch (IOException e) {
            log.error("支付宝付款成功或失败回调失败：{}", "获取回调消息内容错误!");
            log.error(e.getMessage(), e);
            return "fail";
        }
        PayNotifyDataDTO payNotifyDataDTO = PayNotifyDataDTO.builder().body(body).build();
        PayClient client = AlipayPayClientConfig.getH5PayClient();
        PayRefundNotifyDTO payRefundNotifyDTO = client.parseRefundNotify(payNotifyDataDTO);
        if (payRefundNotifyDTO == null) {
            log.error("退款回调失败，解析回调参数为空");
            return "fail";
        }
        PayOrder updateInfo = new PayOrder();
        updateInfo.setNotifyInfo(payNotifyDataDTO.toString());
        updateInfo.setRefundOrderNo(payRefundNotifyDTO.getReqNo());
        updateInfo.setRefundAmount(new BigDecimal(payRefundNotifyDTO.getRefundFee()));
        updateInfo.setSuccessTime(DateUtils.date2LocalDateTime(payRefundNotifyDTO.getRefundSuccessTime()));
        if (PayTradeStatusEnum.SUCCESS.getKey().equals(payRefundNotifyDTO.getStatus())
                || PayTradeStatusEnum.TRADE_SUCCESS.getKey().equals(payRefundNotifyDTO.getStatus())) {
            updateInfo.setStatus(PayOrderStatusEnum.NOTIFY_SUCCESS.getValue());
        } else {
            updateInfo.setStatus(PayOrderStatusEnum.NOTIFY_FAIL.getValue());
        }
        //获取原请求支付订单
        PayOrder dbPayOrder = this.payOrderMapper.selectByOrderCode(payRefundNotifyDTO.getTradeNo());
        if (dbPayOrder != null) {
            updateInfo.setPayOrderID(dbPayOrder.getPayOrderID());
            this.payOrderMapper.updateByPrimaryKeySelective(updateInfo);
        } else {
            log.info("没有找到原始订单");
            updateInfo.setOrderCode(payRefundNotifyDTO.getReqNo());
            this.add(updateInfo, new SysAdmin());
        }
        synchronized (payRefundNotifyDTO.getReqNo().intern()) {
            this.payNotifyOrder(payRefundNotifyDTO.getReqNo());
        }
        return "success";
    }
}
